<?php
// session_start();
include "./pdo-plus/src/PDO.class.php";
include "./config.php";
include "./fn.php";
$db=DB::getInstance($database);
if($type=input("type")){
    switch($type){
        case "reg":
            if(input("username") && input("password")){
                $exited=$db->query("SELECT 1 from user where username=?",[input("username")]);
                if($exited){
                    echo json_encode(['state'=>0,"msg"=>"用户名已经存在"]);
                    return;
                }
                $sql="INSERT INTO user(username,introcode,pass) values(?,?,?)";
                $num=$db->query($sql,[input("username"),input("introcode")?:"",md5(input("password"))]);
                echo json_encode(['state'=>$num]);
            }else{
                echo json_encode(['state'=>0]);
            }
            break;
        case "login":
            if(input("username") && input("password")){
                $user=$db->query("select id,username,photo,email,phone,point from user where username=? and pass=?",[input("username"),md5(input("password"))]);
                if($user){
                   $_SESSION['Token']=md5(time().input("username"));
                   setcookie("UserID",$user[0]['id'],time()+3600*24*30*12,"/");
                   setcookie("UserName",$user[0]['username'],time()+3600*24*30*12,"/");
                   setcookie("Photo",$user[0]['photo'],time()+3600*24*30*12,"/");
                   echo json_encode(['state'=>1,"data"=>["token"=>$_SESSION['Token'],"userid"=>$user[0]['id']]]);
                }else{
                    echo json_encode(['state'=>0]);
                }
            }else{
                echo json_encode(['state'=>0]);
            }
            break;
        case "edituser":
            if(input("userid") && input("password")){
                $user=$db->query("select id,username,photo,email,phone,point from user where id=? and pass=?",[input("userid"),md5(input("password"))]);
                if($user){
                  $sql="UPDATE user set nickname=? ,email=?,phone=? where id=?";
                  $num=$db->query($sql,[input("nickname"),input("email"),input("phone"),input("userid")]);
                  echo json_encode(['state'=>$num]);
                }else{
                    echo json_encode(['state'=>0,"msg"=>"密码不正确"]);
                }
            }
            break;
        case "getfangs":

            break;
        case "addfang":
            $insert=$db->query("INSERT INTO fang(xiaoqu,userid,fangshi,ditie,price,paytype,zoneid,lou,danyuan,fanghao,dianti,chewei,shi,ting,chu,wei,mianji,zhuangxiu,cengthis,cengall,zutype,tese,fengge,kanfang,chaoxiang,dizhi,info,mainimg,imgs,reward,starttime,contact,phone,x,y) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",[input("xiaoqu"),$_COOKIE['UserID']?:0,0,input("ditie")?:0,input("price")?:0,input("paytype")?:0,input("zoneid")?:0,input("lou")?:0,input("danyuan")?:0,input("fanghao")?:0,input("dianti")?:0,input("chewei")?:0,input("shi")?:0,input("ting")?:0,input("chu")?:0,input("wei")?:0,input("mianji")?:0,input("zhuangxiu")?:0,input("cengthis")?:0,input("cengall")?:0,input("zutype")?:0,input("tese")?:0,input("fengge")?:0,input("kanfang")?:0,input("chaoxiang")?:0,"",input("info")?:0,input("mainimg"),input("imgs"),input("reward")?:0,strtotime(input("startime")),input("contact")?:0,input("phone"),input("x"),input("y")]);
            echo json_encode(['state'=>$insert]);
            break;
        default:
            echo $_COOKIE['UserID'];
            break;
    }
}